1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
"use client";
import * as React from "react";
import { useClientTable, ClientVirtualTable } from "@/components/client-table-v3";
import { productColumns } from "../table-v2/columns";
import {
getProductTableData,
getAllProducts,
getProductTableDataWithGrouping,
GroupInfo
} from "../table-v2/actions";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { ChevronDown, ChevronRight } from "lucide-react";
// --- Components for Examples ---
function ClientSideExample() {
const [products, setProducts] = React.useState<any[]>([]);
// Load initial data once
React.useEffect(() => {
getAllProducts().then(setProducts);
}, []);
// Hook handles table state
const { table, isLoading } = useClientTable({
fetchMode: "client",
data: products,
columns: productColumns,
enablePagination: true,
enableGrouping: true,
});
return (
<Card>
<CardHeader>
<CardTitle>Pattern 1: Client-Side (V3)</CardTitle>
<CardDescription>
Uses `useClientTable` hook for simplified state management.
</CardDescription>
</CardHeader>
<CardContent className="h-[500px]">
<ClientVirtualTable
table={table}
isLoading={isLoading}
enableUserPreset
tableKey="v3-client-pattern"
/>
</CardContent>
</Card>
);
}
function ServerFactoryExample() {
// Hook handles everything: state, fetching, debouncing
const { table, isLoading } = useClientTable({
fetchMode: "server",
fetcher: getProductTableData,
columns: productColumns,
enablePagination: true,
enableUserPreset: true, // We can enable this in options too? No, hook doesn't care. Component cares.
});
return (
<Card>
<CardHeader>
<CardTitle>Pattern 2: Factory Service (V3)</CardTitle>
<CardDescription>
Zero boilerplate state management in the component.
</CardDescription>
</CardHeader>
<CardContent className="h-[500px]">
<ClientVirtualTable
table={table}
isLoading={isLoading}
enableUserPreset
tableKey="v3-server-pattern"
/>
</CardContent>
</Card>
);
}
function ServerGroupingExample() {
// Adapter for V2 fetcher signature to work with V3 hook
// The V2 action expects (state, expandedGroups), but V3 hook passes (state).
// We wrap it to extract expandedGroups from state.expanded.
const fetcher = React.useCallback((state: any) => {
const expanded = state.expanded || {};
// Convert TanStack ExpandedState { [key]: true } to string[]
const expandedKeys = Object.keys(expanded).filter(k => expanded[k]);
return getProductTableDataWithGrouping(state, expandedKeys);
}, []);
// Pattern 2-B support
const {
table,
isLoading,
isServerGrouped,
serverGroups,
refresh,
} = useClientTable({
fetchMode: "server",
fetcher,
columns: productColumns,
enablePagination: true,
enableGrouping: true,
});
// When serverGroups change (new grouping), reset expansion
// (In a real app, you might want to persist expansion logic in the fetcher wrapper or hook)
return (
<Card>
<CardHeader>
<CardTitle>Pattern 2-B: Server Grouping (V3)</CardTitle>
<CardDescription>
Hook manages state, Component manages Custom Rendering for Groups.
</CardDescription>
</CardHeader>
<CardContent className="h-[500px] flex flex-col">
{/* We need the toolbar even in grouped mode */}
<div className="mb-4 p-2 border rounded bg-muted/20">
<p className="text-sm text-muted-foreground">
Group by a column (Category, Status, IsNew) to see server grouping.
</p>
</div>
{isServerGrouped ? (
<div className="overflow-auto border rounded-md p-4 space-y-2">
{serverGroups.map((group: GroupInfo) => (
<div key={group.groupKey} className="border rounded p-2">
<div className="font-bold flex items-center gap-2">
<Badge variant="outline">{String(group.groupValue)}</Badge>
<span>({group.count})</span>
</div>
{/* Rows would go here */}
</div>
))}
</div>
) : (
<ClientVirtualTable
table={table}
isLoading={isLoading}
enableUserPreset
tableKey="v3-server-grouping"
/>
)}
</CardContent>
</Card>
);
}
export default function TableV3Page() {
return (
<div className="container py-8 space-y-8">
<div>
<h1 className="text-3xl font-bold">ClientVirtualTable V3 DX Demo</h1>
<p className="text-muted-foreground">
Demonstrating the new `useClientTable` hook for improved Developer Experience.
</p>
</div>
<Tabs defaultValue="client">
<TabsList>
<TabsTrigger value="client">Client-Side</TabsTrigger>
<TabsTrigger value="server">Server Factory</TabsTrigger>
<TabsTrigger value="grouping">Server Grouping</TabsTrigger>
</TabsList>
<TabsContent value="client">
<ClientSideExample />
</TabsContent>
<TabsContent value="server">
<ServerFactoryExample />
</TabsContent>
<TabsContent value="grouping">
<ServerGroupingExample />
</TabsContent>
</Tabs>
</div>
);
}
|